package com.demo.utils;

import java.util.Random;

public class CodeUtils {
    /**
     * 生成随机验证码
     * @param length 需要的长度
     * @return 返回随机验证码
     */
    public static String generateCode(int length) {
        String chars = "0123456789";
        StringBuilder code = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            code.append(chars.charAt(random.nextInt(chars.length())));
        }
        return code.toString();
    }
    /**
     * 生成英文数字随机验证码
     * @param length 需要的长度
     * @return 返回随机验证码
     */
    public static String generateAllCode(int length) {
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder code = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            code.append(chars.charAt(random.nextInt(chars.length())));
        }
        return code.toString();
    }
}
